package cn.gailvlun.gll.presentation.xroom.vm

import android.arch.lifecycle.ViewModel
import cn.gailvlun.gll.chatlib.db.ChatDB2
import cn.gailvlun.gll.chatlib.bean.CrossMessage
import cn.gailvlun.gll.chatlib.bean.CrossMessageType
import cn.gailvlun.gll.net.cross.CrossRoomInfo
import cn.gailvlun.gll.util.ProfileUtil
import io.realm.Realm
import io.realm.RealmResults
import java.util.*

class CrossRoomChatViewModel : ViewModel {

    private var realm: Realm? = null
    var oppositeId: Int? = null
    var gameId: Int? = null
    var crossRoomInfo: CrossRoomInfo? = null

    constructor() : super() {
        realm = Realm.getInstance(ChatDB2.getConfiguration())
    }

    fun init(gameId: Int) {
        this.gameId = gameId
        crossRoomInfo = realm?.where(CrossRoomInfo::class.java)
                ?.equalTo("id", gameId)
                ?.findFirst()
    }

    fun getChat(): RealmResults<CrossMessage>? {
        return realm?.where(CrossMessage::class.java)
                ?.equalTo("crossRoomInfo.id", gameId)
                ?.findAll()
    }

    override fun onCleared() {
        realm?.close()
        realm = null

        super.onCleared()
    }

    fun saveBeforeOpenSystemMsg(): CrossMessage {
        val crossMessage = CrossMessage()
        crossMessage.created = Date()
        crossMessage.oppositeId = oppositeId!!

        crossMessage.type = CrossMessageType.BEFORE_OPEN
        crossMessage.crossRoomInfo = crossRoomInfo

        realm?.executeTransaction {
            it.insertOrUpdate(crossMessage)
        }
        return crossMessage
    }

    fun saveRiddle(round: Int): CrossMessage {
        val crossMessage = CrossMessage()
        crossMessage.created = Date()
        crossMessage.oppositeId = oppositeId!!
        crossMessage.type = CrossMessageType.QUESTION
        crossMessage.sender = if (round == 0) {
            crossRoomInfo?.first?.owner
        } else {
            crossRoomInfo?.second?.owner
        }
        crossMessage.crossRoomInfo = crossRoomInfo
        crossMessage.round = round

        realm?.executeTransaction {
            it.insertOrUpdate(crossMessage)
        }
        return crossMessage
    }

    fun saveAnswerStartSystemMsg(round: Int): CrossMessage {
        val crossMessage = CrossMessage()
        crossMessage.created = Date()
        crossMessage.oppositeId = oppositeId!!
        crossMessage.type = CrossMessageType.ANSWER_START
        crossMessage.crossRoomInfo = crossRoomInfo
        crossMessage.round = round

        realm?.executeTransaction {
            it.insertOrUpdate(crossMessage)
        }
        return crossMessage
    }

    fun saveAnswerDoneSystemMsg(round: Int): CrossMessage {
        val crossMessage = CrossMessage()
        crossMessage.created = Date()
        crossMessage.oppositeId = oppositeId!!
        crossMessage.type = CrossMessageType.ANSWER_DONE
        crossMessage.crossRoomInfo = crossRoomInfo
        crossMessage.round = round

        realm?.executeTransaction {
            it.insertOrUpdate(crossMessage)
        }
        return crossMessage
    }

    fun isRiddleManBy(round: Int): Boolean {
        return if (round == 0) {
            crossRoomInfo?.first?.owner?.id == ProfileUtil.getUserId()
        } else {
            crossRoomInfo?.second?.owner?.id == ProfileUtil.getUserId()
        }
    }

    fun isAnswerManBy(round: Int) : Boolean {
        return if (round == 0) {
            crossRoomInfo?.second?.owner?.id == ProfileUtil.getUserId()
        } else {
            crossRoomInfo?.first?.owner?.id == ProfileUtil.getUserId()
        }
    }

    fun saveAnswerResultSystemMsg(round: Int): CrossMessage {
        val crossMessage = CrossMessage()
        crossMessage.created = Date()
        crossMessage.oppositeId = oppositeId!!
        crossMessage.type = CrossMessageType.ANSWER_RESULT
        crossMessage.crossRoomInfo = crossRoomInfo
        crossMessage.round = round

        realm?.executeTransaction {
            it.insertOrUpdate(crossMessage)
        }
        return crossMessage
    }

    fun saveAnswerCountDownSystemMsg(round: Int): CrossMessage {
        val crossMessage = CrossMessage()
        crossMessage.created = Date()
        crossMessage.oppositeId = oppositeId!!
        crossMessage.type = CrossMessageType.LAST_TEN_SECOND
        crossMessage.crossRoomInfo = crossRoomInfo
        crossMessage.round = round

        realm?.executeTransaction {
            it.insertOrUpdate(crossMessage)
        }
        return crossMessage
    }

    fun savePunishCountDownSystemMsg(round: Int): CrossMessage {
        val crossMessage = CrossMessage()
        crossMessage.created = Date()
        crossMessage.oppositeId = oppositeId!!
        crossMessage.type = CrossMessageType.LAST_TEN_SECOND
        crossMessage.crossRoomInfo = crossRoomInfo
        crossMessage.round = round

        realm?.executeTransaction {
            it.insertOrUpdate(crossMessage)
        }
        return crossMessage
    }

    fun getMessage(msg: String): CrossMessage {
        val crossMessage = CrossMessage()
        crossMessage.created = Date()
        crossMessage.oppositeId = oppositeId!!
        crossMessage.type = CrossMessageType.TEXT
        crossMessage.crossRoomInfo = crossRoomInfo
        crossMessage.text = msg
        crossMessage.sender = ProfileUtil.getProfile()




        return crossMessage
    }

    fun save(crossMessage: CrossMessage) {
        realm?.executeTransaction {
            it.insertOrUpdate(crossMessage)
        }
    }

    fun getImageMessage(localImagePath: String?, width: Int, height: Int): CrossMessage {
        val crossMessage = CrossMessage()
        crossMessage.created = Date()
        crossMessage.oppositeId = oppositeId!!
        crossMessage.type = CrossMessageType.IMAGE
        crossMessage.crossRoomInfo = crossRoomInfo
        crossMessage.localImage = localImagePath
        crossMessage.sender = ProfileUtil.getProfile()
        crossMessage.width = width
        crossMessage.height = height




        return crossMessage
    }

    fun getOppositeName(): String? {
        return if (crossRoomInfo?.first?.owner?.id == ProfileUtil.getUserId()) {
            crossRoomInfo?.second?.owner?.name
        } else {
            crossRoomInfo?.first?.owner?.name
        }
    }

    fun getCrossMessage(messageId: String?): CrossMessage? {
        return realm?.where(CrossMessage::class.java)
                ?.equalTo("id", messageId)
                ?.findFirst()
    }
}